package solutions.leetcode.normal

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/count-nice-pairs-in-an-array/">1814. 统计一个数组中好对子的数目</a>
 */
class Solution1814 {
    fun countNicePairs(nums: IntArray): Int {
        val map = HashMap<Int, Int>()
        var count = 0
        val mod = 1000000007
        for (item in nums) {
            val offset = item - rev(item)
            val c = map.getOrDefault(offset, 0)
            count = (count + c).mod(mod)
            map[offset] = (c + 1).mod(mod)
        }
        return count
    }

    private fun rev(value: Int): Int {
        var num = value
        var rst = 0
        while (num > 0) {
            rst *= 10
            rst += num % 10
            num /= 10
        }
        return rst
    }
}

fun main() {
    println(Solution1814().countNicePairs(arrayOf(
        13,10,35,24,76
    ).toIntArray()))
}